package com.eml.service.dao.impl;

import com.eml.service.dao.AddressMerDao;
import com.eml.service.entity.AddressMer;
import com.eml.service.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AddressMerDaoImpl implements AddressMerDao {
    //查看商家地址
    @Override
    public AddressMer selectAddress(String merchantName) throws Exception {
        Connection conn = JDBCUtils.getConnection();
        String sql="select address,number from address_mer where merchant_name=?";
        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setString(1,merchantName);
        ResultSet rs = psmt.executeQuery();
        if (rs.next()){
            String address = rs.getString("address");
            String number = rs.getString("number");
            JDBCUtils.close(conn,psmt,rs);
            return new AddressMer(address,number);
        }
        JDBCUtils.close(conn,psmt,rs);
        return null;
    }

    //修改商家地址
    @Override
    public int updateAddress(String merchantName, String address, String number) throws Exception {
        Connection conn = JDBCUtils.getConnection();
        String sql="update address_mer set address=?,number=? where merchant_name=?";
        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setString(1,address);
        psmt.setString(2,number);
        psmt.setString(3,merchantName);
        return psmt.executeUpdate();
    }

    @Override
    public int addAddress(String address, String number) throws Exception {
        Connection conn = JDBCUtils.getConnection();
        String sql="insert into address_mer (address,number)values(?,?)";
        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setString(1,address);
        psmt.setString(2,number);
        psmt.executeUpdate();
        JDBCUtils.close(conn,psmt);
        return 0;
    }



}
